package com.automannn.springboot.practice.springApplicationRunListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.*;
import org.springframework.boot.context.event.EventPublishingRunListener;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.metrics.ApplicationStartup;

import java.util.ArrayList;

/**
 * @author chenkh
 * @time 2021/10/8
 */
public class SpringApplicationRunListenerTest {
    public static void main(String[] args) {
        SpringApplication springApplication= new SpringApplication();
        SpringApplicationRunListener springApplicationRunListener = new EventPublishingRunListener(springApplication,new String[]{"aa","bb","cc"});
        GenericApplicationContext applicationContext = new GenericApplicationContext();
        applicationContext.addApplicationListener(new ApplicationListener<ApplicationEvent>() {
            @Override
            public void onApplicationEvent(ApplicationEvent event) {
                System.out.println("got a new Message:"+event.getSource());
            }
        });

        DefaultBootstrapContext bootstrapContext = new DefaultBootstrapContext();
        bootstrapContext.addCloseListener(new ApplicationListener<BootstrapContextClosedEvent>() {
            @Override
            public void onApplicationEvent(BootstrapContextClosedEvent event) {
                System.out.println("got a new  BootstrapContextClosedEvent:"+event.getSource());
            }
        });
        springApplicationRunListener.starting(bootstrapContext);
        springApplicationRunListener.environmentPrepared(new StandardEnvironment());

        springApplicationRunListener.contextPrepared(applicationContext);
        springApplicationRunListener.contextLoaded(applicationContext);

//        springApplicationRunListener.started(applicationContext);
        Log log = LogFactory.getLog(SpringApplicationRunListeners.class);
        ArrayList arrayList = new ArrayList();
        arrayList.add(springApplicationRunListener);
        ApplicationStartup applicationStartup = ApplicationStartup.DEFAULT;
        SpringApplicationRunListeners springApplicationRunListeners = new SpringApplicationRunListeners(log,arrayList,applicationStartup);

        springApplicationRunListeners.contextPrepared(applicationContext);
    }
}
